package com.mlh.handler.exception;

import com.mlh.enums.AppHttpCodeEnum;
import com.mlh.exception.SystemException;
import com.mlh.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 缪林辉
 * @date 2024/3/15 17:10
 * @DESCRIPTION
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    public ResponseResult systemExceptionHandler(SystemException systemException){
        //打印异常信息
        log.error("出现了异常！ {}",systemException);
        //从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(systemException.getCode(),systemException.getMsg());
    }

    //还有可能会出现意料之外的异常，这类异常也需要处理
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e){
        log.error("出现了异常！ {}",e);
        //打印了具体的错误信息e.getMessage() ，方便调试
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
    }
}
